package lg.minecraft.plugin.entityclear.command;

import lg.minecraft.plugin.entityclear.manager.EntityTaskManager;
import lg.minecraft.plugin.entityclear.task.EntityClearTask;
import lg.minecraft.plugin.library.command.CommandContext;
import lg.minecraft.plugin.library.command.SubCommand;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 设置任务清理时间的命令
 */
public class SetCommand extends SubCommand {

    public SetCommand() {
        super("set");
    }

    @Override
    public String getDescription() {
        return "临时设置任务的清理剩余时间";
    }

    @Override
    public void onCommand(CommandContext context) {
        CommandSender sender = context.getSender();
        if (context.getArgLength() == 0) {
            sender.sendMessage("用法: /lgec set <任务名> <时间>");
            return;
        }

        String taskName = context.getArg(0);
        EntityClearTask task = EntityTaskManager.INSTANCE.getTask(taskName);

        if (task == null) {
            sender.sendMessage("[§a实体清理§f] 未找到任务: " + taskName);
            return;
        }

        if (context.getArgLength() <= 1) {
            sender.sendMessage("[§a实体清理§f] 缺少时间");
            return;
        }

        int time;
        String timeString = context.getArg(1);
        try {
            time = Integer.parseInt(timeString);
        } catch (NumberFormatException e) {
            sender.sendMessage("[§a实体清理§f] 时间参数错误: " + timeString);
            return;
        }

        task.setNextClearTime(System.currentTimeMillis() + (time * 1000L));
        sender.sendMessage("[§a实体清理§f] 已临时设置任务 '" + taskName + "' 的清理剩余时间: " + time + " 秒");
    }

    @Override
    public List<String> onTabComplete(CommandContext context) {
        if (context.getArgLength() == 1) {
            return new ArrayList<>(EntityTaskManager.INSTANCE.getKeySet());
        } else if (context.getArgLength() == 2) {
            return Arrays.asList("1", "5", "10", "30", "60");
        }
        return null;
    }

}
